<?php

namespace app\site\controller\store;

use app\common\exception\MiException;
use app\Request;
use app\site\controller\AController;
use app\site\service\store\CouponService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class CouponController extends AController
{


    public function options(): \think\response\Json
    {
        $list =CouponService::instance()->setField("id,title,site_id,is_show")->setWhere([
            ['is_show', '=', 1]
        ])->select();

        return jsonSuccess([
            'options' => $list,
        ]);
    }


    /**
     * @throws DbException
     */
    public function index(){
        return $this->reqSer->setService(CouponService::instance())->setPaginate()->getResponse();
    }

    /**
     * @throws MiException
     */
    public function add(){
        return $this->reqSer->setService(CouponService::instance())->add()->getResponse();
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function edit(Request $request){
        if($request->isPost()){
            return $this->reqSer->setService(CouponService::instance())->edit()->getResponse();
        }else{
            return $this->reqSer->setService(CouponService::instance())->setFind()->getResponse();
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del(){
        return $this->reqSer->setService(CouponService::instance())->delete()->getResponse();
    }

}
